<%--
  Created by IntelliJ IDEA.
  User: 王猛
  Date: 2020/2/20
  Time: 17:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%--
  Created by IntelliJ IDEA.
  User: 王猛
  Date: 2020/2/20
  Time: 14:42
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="UTF-8">

<%@include file="include-head.jsp"%>
<body>

<%@include file="include-nav.jsp"%>
<div class="container-fluid">
    <div class="row">


        <%@include file="include-sidebar.jsp"%>


        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <h1 class="page-header">控制面板</h1>

            <div class="row placeholders">
                <div class="panel-body">
                    <form action="/admin/query/for/search" class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input value="${param.keyword}" name="keyword" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button type="submit" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button type="button" class="btn btn-primary" style="float:right;" onclick="window.location.href='/admin/to/add/page.html'"><i class="glyphicon glyphicon-plus"></i> 新增</button>

                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr >
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>账号</th>
                                <th>名称</th>
                                <th>邮箱地址</th>
                                <th width="100">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <c:if test="${empty requestScope['PAGE-INFO'].list}">
                                <tr>
                                    <td style="text-align: center;" colspan="6">抱歉！没有符合您要求的查询结果！</td>
                                </tr>
                            </c:if>
                            <c:if test="${!empty requestScope['PAGE-INFO'].list}">
                                <c:forEach items="${requestScope['PAGE-INFO'].list}"  var="admin" varStatus="myStatus">
                                    <tr>
                                        <td>${myStatus.count}</td>
                                        <td><input adminId="${admin.id}" class="itemBox" type="checkbox"></td>
                                        <td>${admin.loginacct}</td>
                                        <td>${admin.username}</td>
                                        <td>${admin.email}</td>
                                        <td>
                                            <button type="button" class="btn btn-success btn-xs"><i class=" glyphicon glyphicon-check"></i></button>
                                           <%-- <button type="button" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></button>--%>
                                            <a href="/admin/to/edit/page?adminId=${admin.id }&pageNum=${requestScope['PAGE-INFO'].pageNum}" class="btn btn-primary btn-xs"><i class=" glyphicon glyphicon-pencil"></i></a>
                                            <button loginacct="${admin.loginacct}" adminId="${admin.id}" type="button" class="btn btn-danger btn-xs uniqueRemoveBtn"><i class=" glyphicon glyphicon-remove uniqueRemoveBtn"></i></button>
                                        </td>
                                    </tr>

                                </c:forEach>
                            </c:if>
                            </tbody>


                            <tr >
                                <td colspan="5" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>
                        </tfoot>
                        </table>
                    </div>
        </div>
    </div>
</div>
            <link rel="stylesheet" href="/static/css/pagination.css">
<script src="/static/jquery/jquery-2.1.1.min.js"></script>
<script src="/static/jquery/jquery.pagination.js"></script>
<script src="/static/jquery/bootstrap.min.js"></script>
<script src="/static/jquery/docs.min.js"></script>

<script type="text/javascript">

    $(function () {


        $(".list-group-item").click(function(){
            if ( $(this).find("ul") ) {
                $(this).toggleClass("tree-closed");
                if ( $(this).hasClass("tree-closed") ) {
                    $("ul", this).hide("fast");
                } else {
                    $("ul", this).show("fast");
                }
            }
        });
        /*初始化分页导航条*/
        initPagination();
        /*实现全选和全部写*/
        $("#summaryBox").click(function () {
            /*attr和prop都是用来修改属性值的方法
            * 但是prop可以对新添加的元素生效,也就是动态元素*/
            $(".itemBox").prop("checked",this.checked);
        });

        /*绑定删除按钮的单击事件*/
        $("#batchRemoveBtn").click(function () {
            /*存储adminid*/
            var adminIdArray=new Array();
            $('.itemBox:checked').each(function () {
                var adminid=$(this).attr("adminId");
                adminIdArray.push(adminid);
            })
            if(adminIdArray.length==0){
            alert("请选择数据");
            return;
            }
            /*获取名称并确认删除*/
            var loginAcctArray=new Array();
            $('.itemBox:checked').each(function () {
                var loginAcct=$(this).parent().next().text();
                loginAcctArray.push(loginAcct)
            })
            var requestBody=JSON.stringify(adminIdArray);
            /*确认删除提示*/
            var as=window.confirm("你真的删除么"+loginAcctArray+"信息吗")
            if(as==false){
            return;
            }
            /*调用ajax*/
            $.ajax({
                url:"/admin/batch/remove",
                type:"post",
                dataType:"JSON",
                data:requestBody,
                contentType:"application/json;charset=UTF-8",
                success:function (ajaxResult) {
                    if(ajaxResult.result=="SUCCESS"){

                        /*刷新页面,保持差查询调教,保存页码*/
                        var keyword=$('[name="keyword"]').val();
                        var pageNum=${requestScope['PAGE-INFO'].pageNum - 1};
                        location.href="/admin/query/for/search?pageNum=${requestScope['PAGE-INFO'].pageNum}&keyword=${param.keyword}"
                    }else{

                    }

                }
            })
        })

        /*绑定单挑按钮单击事件*/
        $(".uniqueRemoveBtn").click(function () {
            /*获取id*/
            var adminId=$(this).attr("adminId");
            var adminIdArray=new Array();
            adminIdArray.push(adminId);
            /*获取loginest账号*/
            var loginAcctArray=new Array();
            var loginAcct=$(this).attr("loginacct");
            /*确认删除提示*/
            loginAcctArray.push(loginAcct)
            var as=window.confirm("你真的删除么"+loginAcctArray+"信息吗")
            if(as==false){
                return;
            }
            var requestBody=JSON.stringify(adminIdArray);
            /*执行删除*/
            $.ajax({
                url:"/admin/batch/remove",
                type:"post",
                dataType:"JSON",
                data:requestBody,
                contentType:"application/json;charset=UTF-8",
                success:function (ajaxResult) {
                    if(ajaxResult.result=="SUCCESS"){

                        /*刷新页面,保持差查询调教,保存页码*/
                        var keyword=$('[name="keyword"]').val();
                        var pageNum=${requestScope['PAGE-INFO'].pageNum - 1};
                        location.href="/admin/query/for/search?pageNum=${requestScope['PAGE-INFO'].pageNum}&keyword=${param.keyword}"
                    }else{

                    }

                }
            })
        })

    });

    function initPagination() {
        // 声明变量存储总记录数
        var totalRecord = ${requestScope['PAGE-INFO'].total};
        // 声明变量存储分页导航条显示时的属性设置
        var paginationProperties = {
            num_edge_entries : 3,        //边缘页数
            num_display_entries : 5,      //主体页数
            callback : pageselectCallback, //回调函数
            items_per_page : ${requestScope['PAGE-INFO'].pageSize},    //每页显示数据数量，就是pageSize
            current_page : ${requestScope['PAGE-INFO'].pageNum - 1},//当前页页码
            prev_text : "上一页",       //上一页文本
            next_text : "下一页"        //下一页文本
        };
        // 显示分页导航条
        $("#Pagination").pagination(totalRecord, paginationProperties);



    }

    // 在每一次点击“上一页”、“下一页”、“页码”时执行这个函数跳转页面
    function pageselectCallback(pageIndex, jq) {
        // pageIndex从0开始，pageNum从1开始
        var pageNum = pageIndex + 1;
        var keyword=$('[name="keyword"]').val();
        // 跳转页面
        window.location.href = "/admin/query/for/search?pageNum="+pageNum+"&keyword=${param.keyword}";
        return false;
    }




</script>
</body>
</html>

